package com.health.controller.manage;


import com.health.common.PageBean;
import com.health.common.QueryParam;
import com.health.common.R;
import com.health.pojo.CheckGroup;
import com.health.pojo.Setmeal;
import com.health.service.SetmealService;
import com.health.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/manage/setmeal")
@RequiredArgsConstructor  //替代@AutoWired
@Api(tags = "套餐管理")
@Slf4j
public class SetmealController {

    private final SetmealService setmealService;

    @ApiOperation(value = "分页条件查询")
    @GetMapping("pageQuery")
    public R pageQuery(QueryParam param) {
        log.info("分页条件查询 {}", param);
        PageBean pageBean = setmealService.pageQuery(param);
        return R.success(pageBean);
    }


    @ApiOperation(value = "添加套餐")
    @PostMapping
    public R addSetmeal(@RequestParam("ids") Integer[] checkGroupIds, @RequestBody Setmeal setmeal) {
        log.info("添加套餐 {} {}", setmeal, checkGroupIds);
        //调用业务层保存数据
        setmealService.addSetmeal(setmeal, checkGroupIds);
        return R.success();
    }


    @ApiOperation("根据id查询套餐信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        log.info("根据id查询套餐信息 {}", id);
        Setmeal setmeal = setmealService.getById(id);
        return R.success(setmeal);
    }


    @ApiOperation("根据id查询套餐关联的检查组信息")
    @GetMapping("/getCheckGroupsById/{id}")
    public R getCheckGroupsById(@PathVariable Integer id) {
        log.info("根据id查询套餐关联的检查组信息 {}", id);
        List<CheckGroup> list = setmealService.getCheckGroupsById(id);
        return R.success(list);
    }

    @ApiOperation("修改套餐")
    @PutMapping
    public R updateSetmeal(@RequestParam("ids") Integer[] checkGroupIds,
                           @RequestBody Setmeal setmeal) {
        log.info("修改套餐 {} {}", setmeal, checkGroupIds);
        setmealService.updateSetmeal(setmeal, checkGroupIds);
        return R.success();
    }

    @ApiOperation("删除套餐")
    @DeleteMapping("/{id}")
    public R deleteSetmeal(@PathVariable Integer id) {
        log.info("删除套餐 {}", id);
        setmealService.deleteSetmeal(id);
        return R.success();
    }


    @ApiOperation("上传图片")
    @PostMapping("/upload")
    public R uploadImg(MultipartFile imgFile) throws IOException {
        log.info("上传图片 {}", imgFile);
        //原始文件名   123232.jpg
        String originalFilename = imgFile.getOriginalFilename();
        //生成一个唯一的文件名，拼上原始文件名的后缀
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "")
                + originalFilename.substring(originalFilename.lastIndexOf("."));
        //调用阿里云工具类上传文件
        String url = AliOssUtil.upload(imgFile.getInputStream(), newFileName);
        return R.success(url);
    }



}
